//处理模型
const {
    successModel,
    failModel
} = require('../model/responseModel')

const {
    getUserOrder,
    updateOrder,
    getUnpayOrder,
    getUntakeOrder,
    getUnfinishedOrder,
    setOrderPayed,
    setOrderReceived,
    cancelOrder,
    getCanceledOrder
    
} = require('../controller/order')


//获取商品相关的路由
const handleOrderRoute = (req, res) => {
    //定义处理路由的逻辑
    const method = req.method;
    switch (method) {
        case 'GET': {
            //获取订单详情
            if (req.path === '/order') {
                return getUserOrder(req.query).then(data => {
                    if (data) {
                        return new successModel(data)
                    } else {
                        return new failModel('获取订单失败')
                    }
                })
            //获取未付款订单
            }else if(req.path === '/order/unpay') {
                return getUnpayOrder(req.query).then(data => {
                    if (data) {
                        return new successModel(data)
                    } else {
                        return new failModel()
                    }
                })
            //获取未收货订单
            }else if(req.path === '/order/untake') {
                return getUntakeOrder(req.query).then(data => {
                    if (data) {
                        return new successModel(data)
                    } else {
                        return new failModel()
                    }
                })
                
            //获取已完成订单
            }else if(req.path === '/order/completed') {
                return getUnfinishedOrder(req.query).then(data => {
                    if (data) {
                        return new successModel(data)
                    } else {
                        return new failModel()
                    }
                })
            //获取已取消订单
            }else if(req.path === '/order/canceled') {
                return getCanceledOrder(req.query).then(data => {
                    if (data) {
                        return new successModel(data)
                    } else {
                        return new failModel()
                    }
                })
            }
            
            else{
                return false;
            }
        };
        case 'POST':{
            //添加订单
            if(req.path === '/order/update'){
                return updateOrder(req.body).then(data => {
                    if (data) {
                        return new successModel(data)
                    } else {
                        return new failModel('更新订单失败')
                    }
                })
            //更新订单为已支付状态
            }else if(req.path === '/order/payed') {
                return setOrderPayed(req.body).then(data => {
                    if (data) {
                        return new successModel(data)
                    } else {
                        return new failModel('获取订单失败')
                    }
                })
            //更新订单为已收获状态
            }else if(req.path === '/order/received') {
                return setOrderReceived(req.body).then(data => {
                    if (data) {
                        return new successModel(data)
                    } else {
                        return new failModel('获取订单失败')
                    }
                })
            //取消订单
            }else if(req.path === '/order/cancel') {
                return cancelOrder(req.body).then(data => {
                    if (data) {
                        return new successModel(data)
                    } else {
                        return new failModel('获取订单失败')
                    }
                })
            }
            
            
            else{
                return false;
            }

        };
    default: {
        return false;
    }
    }

}

module.exports = handleOrderRoute